/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config.javabeans;

import opiam.admin.faare.service.services.triggers.TriggerInterface;

import org.apache.log4j.Logger;

import java.io.Serializable;

import java.util.Enumeration;
import java.util.Properties;


/**
 *
 * This class stores definition of a trigger.
 *
 */
public class JBTrigger implements Serializable
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(JBTrigger.class);

    /** Trigger name. */
    private String name;

    /** Trigger type : "PRE" or "POST". */
    private boolean post;

    /** Trigger operation : CREATE, DELETE, SEARCH, LOAD, or MODIFY. */
    private String opname;

    /** Name of the class implementing trigger. */
    private String classname;

    /** Class implementing trigger. */
    private TriggerInterface plugin;

    /** Trigger specific properties. */
    private Properties param = new Properties();

    /**
     * Gets the name of the class implementing the trigger.
     * @return name of the class
     */
    public String getClassname()
    {
        return classname;
    }

    /**
     * Gets trigger operation type.<br>
     * Operation type may be:<br>
     * <li>TriggersService.TYPE_CREATE for a creation trigger</li>
     * <li>TriggersService.TYPE_DELETE for a deletion trigger</li>
     * <li>TriggersService.TYPE_MODIFY for a modication trigger</li>
     * <li>TriggersService.TYPE_LOAD for an entry load trigger</li>
     *
     * @return trigger type
     */
    public String getOpname()
    {
        return opname;
    }

    /**
     * Sets the name of the class implementing the trigger.
     * @param aclassname The class name to set
     */
    public void setClassname(String aclassname)
    {
        this.classname = aclassname;

        // instancie un objet the ce plugin
        try
        {
            Class cl = Class.forName(classname);
            plugin = (TriggerInterface) cl.newInstance();
        }
        catch (Exception ex)
        {
            _logger.error("pb dans setClassname");
            throw new RuntimeException("pb dans setClassname");
        }
    }

    /**
     * Sets the trigger operation type.<br>
     * Operation type may be:<br>
     * <li>TriggersService.TYPE_CREATE for a creation trigger</li>
     * <li>TriggersService.TYPE_DELETE for a deletion trigger</li>
     * <li>TriggersService.TYPE_MODIFY for a modication trigger</li>
     * <li>TriggersService.TYPE_LOAD for an entry load trigger</li>
     * @param aopname The operation type. to set
     */
    public void setOpname(String aopname)
    {
        this.opname = aopname;
    }

    /**
     * Displays the trigger definition.
     *
     * @return String formatted trigger definition
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;

        buf.append("name = ");
        buf.append(name);
        buf.append(System.getProperty("line.separator"));
        buf.append("isPost = ");
        buf.append(post);
        buf.append(System.getProperty("line.separator"));
        buf.append("opname = ");
        buf.append(opname);
        buf.append(System.getProperty("line.separator"));
        buf.append("classname = ");
        buf.append(classname);
        buf.append(System.getProperty("line.separator"));

        if (param != null)
        {
            buf.append("Properties components of param = ");
            buf.append(System.getProperty("line.separator"));

            Enumeration enum1 = param.propertyNames();
            String key;

            while (enum1.hasMoreElements())
            {
                buf.append("...");
                key = (String) enum1.nextElement();
                buf.append(key);
                buf.append(" = ");
                buf.append(param.getProperty(key));
                buf.append(System.getProperty("line.separator"));
            }
        }

        return buf.toString();
    }

    // end of toString method

    /**
     * Returns the class implementing the trigger.
     * It implements opiam.admin.faare.service.services.triggers.TriggerInterface
     * @return class
     */
    public TriggerInterface getPlugin()
    {
        return plugin;
    }

    /**
     * Returns the trigger specific parameters.
     * @return table of parameters
     */
    public Properties getParam()
    {
        return param;
    }

    /**
     * Adds a trigger specific parameter.
     *
     * @param key parameter name
     * @param value parameter value
     */
    public void addParam(String key, String value)
    {
        param.setProperty(key, value);
    }

    /**
     * Gets trigger name.
     * @return trigger name
     */
    public String getName()
    {
        return name;
    }

    /**
     * Sets the trigger name.
     * @param aname The name to set
     */
    public void setName(String aname)
    {
        this.name = aname;
        param.setProperty("_TRIGGER_NAME", this.name);
    }

    /**
     * Checks whether the trigger is a pre or a post trigger.
     * @return true if it is a post trigger, false if it is a pre trigger.
     */
    public boolean isPost()
    {
        return post;
    }

    /**
     * Sets the post trigger flag.
     * @param apost The flag to set
     */
    public void setPost(boolean apost)
    {
        this.post = apost;
    }
}
